package com.oraking.android.cmm.baseactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.oraking.android.cmm.R;
import com.oraking.android.cmm.util.Toaster;

/**
 * 파일명          : BaseActivity.java 
 * 작성일          : 2011. 9. 29.
 * 작성자          : <a href='http://oraking.springnote.com/' target='_blank'>Teddy</a>
 * 수정내용
 * -------------------------------------------------------------
 *      수정일      |      수정자      |              수정내용              
 * -------------------------------------------------------------
 *              |              |
 */

public class BaseActivity extends Activity {
	
	// 설정값 Start
	protected final static int DIALOG_GPS_SETTING = 0;
	protected final static int DIALOG_CLOSE = 1;

	protected final static int INTENT_REGUEST_CODE_GPS = 0;
	protected final static int INTENT_REGUEST_CODE_DEFAULT = 19830605;
	// 설정값 End
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.baseactivity.BaseActivity.mDialogHandler
	 * @writeDay : 2011. 9. 29. 오후 2:52:47
	 * @Todo  : Dialog를 Handling 하기 위한 Handler
	 */
	protected Handler mDialogHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch(msg.what){
			case DIALOG_GPS_SETTING:			// GPS Setting을 위한 Dialog 띄우기 
				showDialog(DIALOG_GPS_SETTING);
				break;
			case DIALOG_CLOSE:					// 앱종료를 위한 Dialog 띄우기
				showDialog(DIALOG_CLOSE);
				break;
			default:
				break;
			}
		}
	};

	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.baseactivity.BaseActivity.onCreateDialog
	 * @writeDay : 2011. 9. 29. 오후 2:52:47
	 * @Todo  : Dialog를 Handling 하는  Method
	 */
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch (id) {
		case DIALOG_GPS_SETTING:		// GPS Module 실행 시키는 Dialog
			return new AlertDialog.Builder(BaseActivity.this)
			.setTitle(getString(R.string.messagebox_title_notice))
			.setMessage(getResources().getString(R.string.gpssetting))
			.setPositiveButton(getResources().getString(R.string.messagebox_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
					intent.addCategory(Intent.CATEGORY_DEFAULT);
					startActivityForResult(intent, INTENT_REGUEST_CODE_GPS);
				}
			})
			.setNegativeButton(getResources().getString(R.string.messagebox_cancel), null)
			.show();
		case DIALOG_CLOSE:				// App 종료 시키는 Dialog
			return new AlertDialog.Builder(BaseActivity.this)
			.setTitle(getString(R.string.messagebox_title_alert))
			.setMessage(getResources().getString(R.string.mainclosemsg))
			.setPositiveButton(getResources().getString(R.string.messagebox_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			})
			.setNegativeButton(getResources().getString(R.string.messagebox_cancel), null)
			.show();
		default:
			break;
		}
		
		return super.onCreateDialog(id);
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.baseactivity.BaseActivity.mToastHandler
	 * @writeDay : 2011. 9. 29. 오후 2:52:47
	 * @Todo  : Toast를 Handling 하기 위한 Handler
	 */
	Handler mToastHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			Toaster.showMsg(BaseActivity.this, msg.what);
		};
	};
	 
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.baseactivity.BaseActivity.onActivityResult
	 * @writeDay : 2011. 9. 29. 오후 3:10:27
	 * @Todo  : Android에서 기본으로 제공하는 Toast를 사용하지않고 Customizing 하기위한 Method
	 */
	protected void showMsg(int resId){
		mToastHandler.sendEmptyMessage(resId);
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.baseactivity.BaseActivity.onActivityResult
	 * @writeDay : 2011. 9. 29. 오후 3:10:27
	 * @Todo  : onActivityResult Method override
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode == RESULT_OK && data != null){
			switch(requestCode){
			case INTENT_REGUEST_CODE_GPS:
				break;
			default:
				break;
			}
		} else if(resultCode == RESULT_FIRST_USER && data != null){
			setResult(RESULT_FIRST_USER, data);
			finish();
		}
	}
	
	
}
